<template>
    <!-- 搜索表 -->
    <el-form :inline="true" :model="formInline" class="w-full h-10">
        <el-form-item label="名称">
            <el-input v-model="formInline.roomName" placeholder="姓名" />
        </el-form-item>
        <el-form-item label="楼层">
            <el-input v-model="formInline.floor" placeholder="楼层" />
        </el-form-item>        
        <el-form-item>
            <el-button type="primary" @click="onSearch">搜索</el-button>
        </el-form-item>
    </el-form>
    <el-button type="primary" @click="addOne(0)">添加</el-button>
    <el-button type="danger" @click="delMore">批量删除</el-button>
    <div class="w-full">
        <!-- 表格显示 -->
        <el-table :data="tableData" style="width: 100%" border height="60vh" highlight-current-row @row-dblclick="getInfo" @selection-change="selected">
            <el-table-column fixed type="selection" width="50" />
            <el-table-column prop="roomId" label="序号" width="80" align="center"></el-table-column>
            <el-table-column prop="roomName" label="名称"  align="center"></el-table-column>
            <el-table-column prop="floor" label="楼层"  align="center"></el-table-column>
            <el-table-column prop="departmentName" label="对应科室"  align="center"></el-table-column>
            <el-table-column label="操作"  align="center" width="200">
                <template #default="scope">
                    <el-button type="primary" @click="addOne(scope.row)">编辑</el-button>
                    <el-button type="success" @click="getInfo(scope.row)">查看详情</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <!-- 页码 -->
    <div class="flex justify-center items-end w-full h-16 mt-2">
        <el-pagination
            v-model:current-page="pageForm.currentPage"
            v-model:page-size="pageForm.pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageForm.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            />
            <el-switch v-model="background" class="ml-2" />
    </div>
    <!-- 进行编辑添加 -->
    <el-dialog v-model="dialogFormVisible" :title="addEditTitle" width="300">
        <el-form :model="addEditForm" label-width="80px">
            <el-form-item label="名称">
                <el-input v-model="addEditForm.roomName" placeholder="名称" />
            </el-form-item>
            <el-form-item label="楼层">
                <el-input v-model="addEditForm.floor" placeholder="楼层" />
            </el-form-item>
            <el-form-item label="科室">
                <el-input v-model="addEditForm.departmentName" placeholder="科室" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addEdit">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!-- 进行查看详情 -->
    <el-dialog v-model="dialogInfoVisible" title="房间详情" width="300">
        <el-form :model="addEditForm" label-width="80px">
            <el-form-item label="名称">
                <el-input v-model="addEditForm.roomName" placeholder="名称" />
            </el-form-item>
            <el-form-item label="楼层">
                <el-input v-model="addEditForm.floor" placeholder="楼层" />
            </el-form-item>
            <el-form-item label="科室">
                <el-input v-model="addEditForm.department" placeholder="科室" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addEdit">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

</template>

<script setup>
import { ref, reactive, computed} from "vue";
import { ElMessageBox, ElMessage } from 'element-plus'
// 导入axios
import { getList } from "@/api/user/room.js"
// 表数据区
let tableData = reactive([
    // {id : 1, roomName: "1sda", floor: "1", department: "da1"},
    // {id : 2, roomName: "2sd", floor: "2", department: "asd2"},
    // {id : 3, roomName: "3sad", floor: "3", department: "ad3"},
    // {id : 4, roomName: "4asd", floor: "4", department: "asd4"},
    // {id : 5, roomName: "5sda", floor: "5",department: "ad5"},    
])
// 添加编辑
const dialogFormVisible = ref(false)
// 详情
const dialogInfoVisible = ref(false)
// 搜索表
const formInline = reactive({
    roomName: "",
    floor: ""
})
// 页数
const pageForm = reactive({
    currentPage: 1,
    pageSize: 10,
    total: 50
})
// 背景
const background = ref(false)
// 搜索条件
const pageSearch = computed(() => {
  return {
    page: pageForm.currentPage,
    limit: pageForm.pageSize,
    roomName: formInline.roomName,
    floor: formInline.floor,
  }
})
// 编辑或者添加
var addEditForm = reactive({roomName: "", floor: "", department: "", status: 1})
// 选择复选框
let idArr = []
// 编辑添加标题
let addEditTitle = ref("添加房间")
// 判断是添加还是编辑
let addOrEdit = ref(false)
// 方法
initData()

function initData(){
    getList(pageSearch.value).then(res => {
        console.log(res)
        tableData = res.data.list
        pageForm.currentPage = res.data.currPage
        pageForm.total = res.data.totalCount
        pageForm.pageSize = res.data.pageSize
    })
}
const selected = (data) => {
  idArr = []
  data.forEach((value) => {
    idArr.push(value)
  })
  console.log(data)
  console.log(idArr)
}

// 点击搜索
const onSearch = () => {
  initData()
}
// 获取详细信息
function getInfo(row){
    dialogInfoVisible.value = true
    console.log(row)
}

const handleSizeChange = (val) => {
//   console.log(`${val} items per page`)
    initData()
}
const handleCurrentChange = (val) => {
//   console.log(`current page: ${val}`)
    initData()
}
// 删除
function delMore(){
    if(idArr.length == 0){
        ElMessage.error("请选择")
        return
    }
    alert(idArr.length)
    // 进行删除
    ElMessage.success("删除成功")
}
// 进行添加
function addOne(n){
    // console.log(n)
    dialogFormVisible.value = true
    if(n == 0){
        console.log("进行添加")
        addEditTitle = "添加房间"
        addOrEdit = ref(true)
        addEditForm = {roomName: "", floor: "", department: "", status: 1}
    }else{
        console.log("进行编辑")
        addEditTitle = "编辑房间"
        addOrEdit = ref(false)
        addEditForm = computed(()=>{
            return {
                roomId : n.roomId,
                roomName: n.roomName,
                floor: n.floor,
                department: n.department,
            }
        })
    }
}

// 确定
function addEdit(){
    dialogFormVisible.value = false
    console.log("点击了确定")
    console.log(addEditForm)
    if(addOrEdit.value){
        ElMessage.success("添加成功")
    }else{
        ElMessage.success("编辑成功")
    }
    
}
</script>

<style scoped>

</style>